<script type="text/javascript">
function creatNewhref(id, val, bgaId){
	var myId = document.getElementById("box-"+id);
	var myVal = document.createElement("a");
	myVal.href = "javascript:changeAttr('"+id+"','"+bgaId+"')";
	myVal.id = "attr-"+bgaId;
	myVal.innerHTML = val;
	myId.appendChild(myVal);
	var nbsp = document.createTextNode(" "); 
	myId.appendChild(nbsp);
}

function changeBrand(val){
	document.getElementById("brand").value=val;
	//改变页面显示状态。
	document.getElementById("filterattr").submit();
}

function changePrice(min,max){
	document.getElementById("pricemin").value=min;
	document.getElementById("pricemax").value=max;
	//改变页面显示状态。
	document.getElementById("filterattr").submit();
}

function changeAttr(type,id){
	var tattri = document.getElementById("selectedtattri").value;
	var attr = document.getElementById("selectedattr").value;
	var tmpType = new Array();
	var tmpId = new Array();
	var flag = false;
	if(attr.length>0){
		//拆分多个属性
		tmpType = tattri.split(",");
		tmpId = attr.split(",");
		//查找该类属性是否存在
		for(var i in tmpType){
			if(tmpType[i]==type){
				tmpId[i] = id;
				flag = true;
				break;
			}
		}
		//未找到该类属性时，添加属性
		if(!flag){
			tmpType.push(type);
			tmpId.push(id);
		}	
	}else{
		//属性为空时，直接添加
		tmpType.push(type);
		tmpId.push(id);
	}
	var strType = "";
	var strId = "";
	for(var i in tmpType){
		strType+=tmpType[i]+",";
		strId+=tmpId[i]+",";
	}	
	strType = strType.substring(0,strType.length-1);
	strId = strId.substring(0,strId.length-1);
	document.getElementById("selectedtattri").value=strType;
	document.getElementById("selectedattr").value=strId;
	document.getElementById("filterattr").submit();
} 
function checkSelectedVal(){
	var brand = $("#brand").val();
	var pricemin = $("#pricemin").val();
	var pricemax = $("#pricemax").val();
	var attr = $("#selectedattr").val();
	var tattri = $("#selectedtattri").val();
	//设置选中的品牌
	$("#pp-"+brand).attr({"href":"javascript:void(0)","class":"selectedAttr"});
	$("#"+pricemin+"-"+pricemax).attr({"href":"javascript:void(0)","class":"selectedAttr"});
	if(attr.length>0){
		var tmpType = new Array();
		var tmpId = new Array();
		//拆分多个属性
		tmpType = tattri.split(",");
		tmpId = attr.split(",");
		for(var i in tmpId){
			//如果selectedattr中的值不为0，则改变选中的显示状态，还原全部为未选中状态
			if(tmpId[i]!="0"){
				$("#attr-"+tmpId[i]).attr({"href":"javascript:void(0)","class":"selectedAttr"});
				$("#attrall-"+tmpType[i]).attr({"href":"javascript:changeAttr('"+tmpType[i]+"','0')","class":""});
			}
		}
	}
}
 
</script>
<style type="text/css">
	.selectedAttr {
    background: none repeat scroll 0 0 #91D3F7;
    margin: 0 3px;
    padding: 3px 5px;
    text-decoration:none;
    cursor:default;
    }
</style>
[#-- 创建商品筛选条件的自定义宏，参数：var_filterattr(属性)，par_filterbrand（品牌），par_filterprice（价格），par_goodsbrand(已选品牌)，par_pricemin par_pricemax（已选最低最高价）--]
[#macro ecs_filterattr par_filterattr par_filterbrand par_filterprice par_goodsbrand par_pricemin par_pricemax par_selectedattr par_selectedtattri]
[#if par_filterbrand??]
<form method="post" id="filterattr" action="${var_contextpath}/category.xhtml?group=${var_goodsgroup}">
<input type="hidden" name="brand" id="brand" value="${par_goodsbrand}">
<input type="hidden" name="pricemin" id="pricemin" value="${par_pricemin}"> 
<input type="hidden" name="pricemax" id="pricemax" value="${par_pricemax}"> 
<input type="hidden"  name="selectedattr" id="selectedattr" size="300" value="${par_selectedattr}"> 
<input type="hidden"  name="selectedtattri" id="selectedtattri" size="300" value="${par_selectedtattri}"> 
<div class="box">
	<div class="box_1">
		<h3><span>商品筛选</span></h3>
			[#if par_filterbrand??]
			<div class="screeBox">
				<strong>品牌：</strong>
				<a id="pp-0" href="javascript:changeBrand('0')">全部</a>&nbsp;
			[#list par_filterbrand as data]
				<a id="pp-${data.bgbId}" href="javascript:changeBrand('${data.bgbId}')">${data.bgbName}</a>&nbsp;
			[/#list]		
			</div>
			[/#if]
			[#if par_filterprice.pricemin?? || par_filterprice.pricemax??]
				<div class="screeBox">
					<strong>价格：</strong>
					<a id="0-0" href="javascript:changePrice('0','0')">全部</a>&nbsp;
					[#assign pricemin = par_filterprice.pricemin]
					[#assign pricemax = par_filterprice.pricemax]
					[#assign section = par_filterprice.bggSection]
					[#assign val=(pricemax-pricemin)/section ]
					[#--首先处理得到的最低，最高价格--]	
					[#if pricemin lt 100]
						[#assign pricemin = 0]
					[/#if]
					[#if pricemin gt 100]
						[#assign pricemin = (pricemin/100)?int]
						[#assign pricemin = pricemin*100]
					[/#if]
					[#if pricemax lt 100 && pricemax gt 0]
						[#assign pricemax = 100]
					[/#if]
					[#if pricemax gt 100]
						[#if pricemax%100 = 0]
							[#assign pricemax = (pricemax/100)?int]
							[#assign pricemax = pricemax*100]
						[#else]
							[#assign pricemax = ((pricemax/100)?int)+1]
							[#assign pricemax = pricemax*100]
						[/#if]
					[/#if]
					[#--判断分段后的计算值是否大于100--]	
					[#if val lte 100]
						[#assign val = 100]
					[/#if]
					[#if val gt 100]
						[#if val%100 != 0]
							[#assign val = ((val/100)?int)+1]
							[#assign val = val*100]
						[/#if]
					[/#if]	
					[#list 1..section as i]
						<a id="${pricemin}-${pricemin+val}" href="javascript:changePrice('${pricemin}','${pricemin+val}')">${pricemin}&nbsp;-&nbsp;${pricemin+val}</a>&nbsp;
						[#assign pricemin = pricemin+val]
						[#if pricemin gte pricemax]
							[#break]
						[/#if]
					[/#list]
				</div>
			[/#if]
			[#if par_filterattr??]
			[#assign bgaTattri = "0"]
			[#list par_filterattr as data]
				[#if data.bgaTattri!=bgaTattri]
				[#if data.bgaValue!=""]
				<div class="screeBox" id="box-${data.bgaTattri}">
					<strong>${data.btaName}：</strong>
					<a id="attrall-${data.bgaTattri}" href="javascript:changeAttr('${data.bgaTattri}','0')" class="selectedAttr">全部</a>
					<a id="attr-${data.bgaId}" href="javascript:changeAttr('${data.bgaTattri}','${data.bgaId}')">${data.bgaValue}</a>
				</div>
				[/#if]
				[#else]
					<script type="text/javascript">
						creatNewhref("${data.bgaTattri}","${data.bgaValue}","${data.bgaId}");
					</script>
				[/#if]
				[#assign bgaTattri=data.bgaTattri]
			[/#list]
			[/#if]	
	 </div>
</div>
 </form>
 <script type="text/javascript">
 	window.onload = function(){
 		checkSelectedVal();
 	}
 </script>
[/#if]
[/#macro]
